import { StorageUtils } from "@ytsdk/utils/storage_util.js"


export class PageInfo {
    constructor(data) {
        this.pageCode = data["pageCode"] ?? "";
        this.pageType = data["pageType"] ?? "";
        this.pageRoute = data["pageRoute"] ?? "";
        this.pageName = data["pageName"] ?? "";
        this.pageUrl = data["pageUrl"] ?? "";
    }

    //桌面、渠道、通知栏
    static getDefaultOrigin(type) {
        let pageCode, pageName, pageUrl
        switch (type) {
            case "DESK":
                pageCode = "DESK_ROOT"
                pageName = "桌面"
                pageUrl = "桌面"
                break;
            case "CHANNEL":
                pageCode = "CHANNEL_ROOT"
                pageName = "hap链接"
                pageUrl = "hap链接"
                break;
            case "NOTICE":
                pageCode = "NOTICE_ROOT"
                pageName = "通知栏"
                pageUrl = "通知栏"
                break;
        }

        return new PageInfo({
            "pageCode": pageCode,
            "pageName": pageName,
            "pageUrl": pageUrl
        })
    }

    serialize() {
        return {
            "pageCode": this.pageCode,
            "pageType": this.pageType,
            "pageRoute": this.pageRoute,
            "pageName": this.pageName,
            "pageUrl": this.pageUrl
        }
    }
}
const defaultConfig = {
    "/pagesC/Start": new PageInfo({
        "pageCode": "DESK_ROOT",
        "pageType": "一级页面",
        "pageRoute": "/pagesC/Start",
        "pageName": "启动页",
        "pageUrl": "启动页",
    }),
    "/pagesA/Main?selectIndex=0": new PageInfo({
        "pageCode": "MOMO_HOME",
        "pageType": "一级页面",
        "pageRoute": "/pagesA/Main?selectIndex=0",
        "pageName": "首页",
        "pageUrl": "首页",

    }),
    "/pagesA/Main?selectIndex=1": new PageInfo({
        "pageCode": "MOMO_SOLAR",
        "pageType": "一级页面",
        "pageRoute": "/pagesA/Main?selectIndex=1",
        "pageName": "节气页",
        "pageUrl": "节气页",

    }),
    "/pagesA/Main?selectIndex=2": new PageInfo({
        "pageCode": "MOMO_MINE",
        "pageType": "一级页面",
        "pageRoute": "/pagesA/Main?selectIndex=2",
        "pageName": "我的页",
        "pageUrl": "我的页",

    }),
    "/pagesC/Challenge": new PageInfo({
        "pageCode": "MOMO_CHALLENGE",
        "pageType": "二级页面",
        "pageRoute": "/pagesC/Challenge",
        "pageName": "答题页",
        "pageUrl": "答题页",

    }),
    "/pagesC/Knowledge": new PageInfo({
        "pageCode": "MOMO_KNOWLEDGE",
        "pageType": "二级页面",
        "pageRoute": "/pagesC/Knowledge",
        "pageName": "列表页",
        "pageUrl": "列表页",

    }),
    "/pagesB/Customer-Service": new PageInfo({
        "pageCode": "MOMO_CUSTOMER_SERVICE",
        "pageType": "二级页面",
        "pageRoute": "/pagesB/Customer-Service",
        "pageName": "联系客服页",
        "pageUrl": "联系客服页",

    }),
    "/pagesB/Privacy": new PageInfo({
        "pageCode": "MOMO_PRIVACY_USER",
        "pageType": "二级页面",
        "pageRoute": "/pagesB/Privacy",
        "pageName": "隐私协议",
        "pageUrl": "隐私协议",

    }),
    "/pagesB/Setting": new PageInfo({
        "pageCode": "MOMO_SETTING",
        "pageType": "二级页面",
        "pageRoute": "/pagesB/Setting",
        "pageName": "设置",
        "pageUrl": "设置",

    })
}


export class RouteConfig {
    constructor() { }
    //根据路由获取当前页面的页面信息
    static getPageInfoByRoute(pageRoute) {
        return defaultConfig[pageRoute];
    }

    //获取来源页信息
    static async getOriginPageInfo() {
        let data = await StorageUtils.getItem("OriginPageInfo")
        if (data) return new PageInfo(data)

        return ""
    }

    //更新来源页信息
    static async updateOriginPageInfo(pageInfo) {
        if(pageInfo){
            await StorageUtils.setItem("OriginPageInfo", pageInfo.serialize())
        }
    }

    //清空来源页信息
    static async deleteOriginPageInfo() {
        await StorageUtils.deleteItem("OriginPageInfo")
    }
}
